#include "stm32f10x.h"                  // Device header

void Pwm_Init(void)
{
    //使能外设时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//重映像时钟
//	//
//	
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);//定时器2重映像
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//禁用JTAG
	
	//初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出模式
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//
	
	TIM_InternalClockConfig(TIM2);//选择时钟源为RCC内部时钟	 
	
  //初始化时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;//时钟不分割
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
	TIM_TimeBaseInitStructure.TIM_Period=20000-1;//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler=70-1;//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x00;//不预装
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
  //
	
	TIM_Cmd(TIM2,ENABLE);//使能定时器
    
	//配置输出比较单元
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);//初始化为缺省值
	TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//高电平有效
	TIM_OCInitStructure.TIM_OutputState= TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse=10000;//CCR
	TIM_OC2Init(TIM2,&TIM_OCInitStructure);//初始化输出比较
    //
}

void ResetCCR(uint16_t CCR)
{
  TIM_SetCompare2(TIM2,CCR);
}


